//
//  ApiClient.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/17.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ApiClient.h"

@implementation ApiClient
+ (MKNetworkEngine *)engine
{
    static dispatch_once_t onceToken;
    static MKNetworkEngine *shareEngine;
    dispatch_once(&onceToken, ^ {
        shareEngine = [[MKNetworkEngine alloc] initWithHostName:kHOST portNumber:kPORT apiPath:nil customHeaderFields:nil];
        
    });
    return shareEngine;
}

+ (MKNetworkOperation *)requestWithPath:(NSString *)path headers:(NSDictionary *)headers params:(NSDictionary *)params  httpMethod:(NSString*)httpMethod
{
    NSAssert(path!=nil, @"the url path can't be null");
    MKNetworkOperation *operation = [[ApiClient engine] operationWithPath:path params:params httpMethod:(KCNSSTRING_ISEMPTY(httpMethod))?@"GET":httpMethod ssl:NO];
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
    /*post时传入参数contenttype设置有三种类型：
     MKNKPostDataEncodingTypeURL类型代表:application/x-www-form-urlencoded; charset=
     MKNKPostDataEncodingTypeJSON类型代表:application/json; charset=
     MKNKPostDataEncodingTypePlist类型代表:application/x-plist; charset=%@
     */
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    
    if (headers) {
        [operation addHeaders:headers];
    }
    
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
    //    CSHDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}


+ (MKNetworkOperation *)requestWithPath:(NSString *)path headers:(NSDictionary *)headers params:(NSDictionary *)params ssl:(BOOL )ssl
{
    NSAssert(path!=nil, @"the url path can't be null");
    MKNetworkOperation *operation = [[ApiClient engine] operationWithPath:path params:params httpMethod:@"POST" ssl:ssl];
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    if (headers) {
        [operation addHeaders:headers];
    }
    
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
    //    CSHDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}


+ (MKNetworkOperation *)requestWithUrlString:(NSString*)urlString headers:(NSDictionary *)headers params:(NSDictionary *)params  httpMethod:(NSString*)httpMethod
{
    NSAssert(urlString!=nil, @"the url path can't be null");
    
    MKNetworkOperation *operation = [[ApiClient engine] operationWithURLString:urlString params:params httpMethod:(KCNSSTRING_ISEMPTY(httpMethod))?@"POST":httpMethod];
    
    
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeURL];
    if (headers) {
        [operation addHeaders:headers];
    }
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
    //    QLKDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}

@end
